package Domain.forumManager;

import java.util.Vector;

import objects.Frm;
import objects.Message;
import objects.Trd;
import objects.User;
import DB.DB;

public class ForumsManager
{
	private DB _database;
	public ForumsManager(DB database)
	{
		this._database=database;
	}
	
	
	/**
	 * 
	 * @param nameOfForum
	 * @param nameOfUser
	 * @return -1 if can't find user , -2 if user is a default user  , 1 if forum added
	 */
	public int addForum (String nameOfForum,String nameOfUser)
	{
		User u=this._database.findUser(nameOfUser);
		if (u==null)
		{
			System.out.println("can't find user "+nameOfUser);
			return -1;
		}
		
		else {
			
			
			if(u.getTypeOfUser()!=User.Type.MANAGER)
			{
				return -2;
				
			}else
				{
					this._database.addForum(nameOfForum);
					return 1;
				}
		}
		
	}
	
	
	/**
	 * 
	 * @param nameOfForum
	 * @param nameOfUser
	 * @param nameOfThread
	 * @return -1 if user not found ,-2 if user doesn't a manager ,-3 if forum doesn't exist
	 * 1 if thread added
	 */
	public int addThread (String nameOfForum,String nameOfUser,String nameOfThread)
	{
		User u=this._database.findUser(nameOfUser);
		if (u==null)
		{
			System.out.println("can't find user "+nameOfUser);
			
			return -1;
		}
		
		
		if(u.getTypeOfUser()!=User.Type.MANAGER)
		{
			System.out.println("Default user cannot open threads");
			return -2;
			
		}else
		{
			if (this._database.addThreadToFroum(nameOfForum, nameOfThread)==null)
			{
				System.out.println("the forum "+ nameOfForum+" doesn't exist");
				return -3;
			}
		}
		return 1;
		
	}
	
	
	
	
	/**
	 * how is use this function  need to send the announsment to all of the User Friends
	 * @param nameOfForum the name of the forum to add the message
	 * @param nameOfUser the name of the User that added the message
	 * @param nameOfThread the name of the thread to add the message
	 * @param msg the msg to be added
	 * @return the vector of the friend to send them the message
	 */
	public Vector <User> addMessage (String nameOfForum,String nameOfUser,String nameOfThread,String msg)
	{
		User u=this._database.findUser(nameOfUser);
		if (u==null)
		{
			System.out.println("can't find user "+nameOfUser);
			return null;
		}
		
		try{
		this._database.findFroum(nameOfForum).addMessageToThread(nameOfThread,u, msg);
		}catch(NullPointerException e)
		{
			return null;
		}
		
		return u.getFrindList();
		
		
	}
	/**
	 * 
	 * @param nameOfUser
	 * @param nameOfForum
	 * @param nameOfThread
	 * @param titleOfMessage
	 * @param newcontent
	 * @return -2 if thread doesn't found , return -1 if msg doesn't found , -3 if user doesn't found
	 *  -4 if forum doesn't found ,-5 unkown problem,return ,-6 if user not owner or manager,1 on seccss  
	 */
	public int editMessage (String nameOfUser, String nameOfForum , String nameOfThread, String titleOfMessage,String newContent)
	{
		User u=this._database.findUser(nameOfUser);
		if(u==null)
		{
			return -3;
		}
		try{
			Frm f=this._database.findFroum(nameOfForum);
			if (f==null)
				return -4;
			
			return f.editMessageInThread(u,nameOfThread, titleOfMessage, newContent);
			}catch(NullPointerException e)
			{
				return -5;
			}
			
			
	}
	
	
	/**
	 * 
	 * @param nameOfForum
	 * @param nameOfUser
	 * @param nameOfThread
	 * @param msg
	 * @return -5 if user not owner nor manager,-4 if fail to find user, /
	 * -3 if can't find forum ,-2 if thread doesn't found ,/ 
	 *return -1 if message doesn't found ,/
	 * return 1 if object remove
	 */
	public int removeMessage(String nameOfForum,String nameOfUser,String nameOfThread,String msg)
	{
		User u=this._database.findUser(nameOfUser);
		if(u==null)
		{
			System.out.println("can't find user "+nameOfUser);
			return -4;
		}
		User u1=this._database.findOwnerOfMessage(nameOfForum, nameOfThread, msg);
		System.out.println("WE ARE HERE before if-----------------");
		if (u1.equals(u)||u1.getTypeOfUser()==User.Type.MANAGER)
		{
			System.out.println("WE ARE HERE in if -----------------");
			Frm f=this._database.findFroum(nameOfForum);
			if (f==null)
				return -3;
			return f.removeMessageFromThread(nameOfThread, msg);
			
		}
		else return -5;
		
	}
	
	
	public Vector<Trd> getThreads(String nameOfForum)
	{
		
		return this._database.getThreads(nameOfForum);
	}
	public Vector<Message> getMessages(String nameOfForum, String nameOfThread)
	{
		return this._database.getMessageFromThread(nameOfForum, nameOfThread);
	}
	

	public Vector<Frm> getForums ()
	{
		return this._database.get_forum();
	}
	
	

}
